package com.hxalex.simulationhandle.ui.home.adpter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.databinding.LayoutItemTaskBinding;
import com.hxalex.simulationhandle.ui.home.model.TaskBean;

import java.util.List;

/**
 * Created by hx_Alex on 2018/5/22.
 */

public class TaskAdpter extends RecyclerView.Adapter<TaskAdpter.ItemTaskViewHodler> {
   private List<TaskBean> mData;
   private Context mContext;
   private LayoutInflater inflater;
   private onClickListener onClickListener;
   private onLongClickListener onLongClickListener;

    public TaskAdpter(List<TaskBean> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
        inflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public List<TaskBean> getmData() {
        return mData;
    }

    public void setmData(List<TaskBean> mData) {
        this.mData = mData;
    }

    public void setOnClickListener(TaskAdpter.onClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnLongClickListener(TaskAdpter.onLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    @Override
    public ItemTaskViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutItemTaskBinding mbinding = DataBindingUtil.inflate(inflater,R.layout.layout_item_task,parent,false);
        return new ItemTaskViewHodler(mbinding);
    }

    @Override
    public void onBindViewHolder(ItemTaskViewHodler holder, int position) {
        if(mData!=null && mData.get(position)!=null)
            holder.getmBinding().setTaskBean(mData.get(position));
            holder.getmBinding().getRoot().setOnClickListener(v->
            {
                onClickListener.onClick(holder.getmBinding().getRoot(),position,mData.get(position));
            });
        holder.getmBinding().getRoot().setOnLongClickListener(v->
        {
            onLongClickListener.longClick(holder.getmBinding().getRoot(),position,mData.get(position));
            return false;   
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class  ItemTaskViewHodler extends RecyclerView.ViewHolder
    {
        private LayoutItemTaskBinding mBinding;
        public ItemTaskViewHodler(LayoutItemTaskBinding mBinding) {
            super(mBinding.getRoot());
            this.mBinding=mBinding;
        }


        public LayoutItemTaskBinding getmBinding() {
            return mBinding;
        }
    }
    
   public interface  onClickListener
    {
       void onClick(View view,int position,TaskBean taskBean);
       
       
    }

    public interface  onLongClickListener
    {
        void longClick(View view,int position,TaskBean taskBean);
    }
}
